package com.tth.learn.addproperties.java;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @Author tth
 * @Date 2022/12/7 14:37
 */
public class Java8DateTest {
    public static void main(String[] args) {
        // 实例化1：使用静态方法now()，获取当前日期/时间/时间+日期
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        // 实例化2：使用静态方法of()，设置指定的时间。没有偏移量
        LocalDate localDate1 = LocalDate.of(2022, 12, 7);
        LocalTime localTime1 = LocalTime.of(14, 44, 12);
        LocalDateTime localDateTime1 = LocalDateTime.of(2022, 12, 7, 14, 42, 33);
        System.out.println(localDate1);
        System.out.println(localTime1);
        System.out.println(localDateTime1);

        // 2022-12-7 14:47:28
        // getXXX()：获取相关属性
        System.out.println(localDateTime.getDayOfMonth()); // 7
        System.out.println(localDateTime.getDayOfWeek()); // WEDNESDAY
        System.out.println(localDateTime.getMonth()); // DECEMBER
        System.out.println(localDateTime.getMonthValue()); // 12
        System.out.println(localDateTime.getMinute()); // 47

        // withXxx()：设置相关属性 ---> 体现了不可变性，原时间不变，时间修改方法中有返回值，返回的新日期是被修改过的
        LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);
        System.out.println(localDateTime); // 2022-12-07T14:54:38.985
        System.out.println(localDateTime2); // 2022-12-22T14:54:38.985


        // plusXxx()：对相关属性执行“+”操作
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime); // 2022-12-07T14:54:38.985
        System.out.println(localDateTime3); // 2023-03-07T14:54:38.985

        // minusXxx()：对相关属性执行“-”操作
        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println(localDateTime); // 2022-12-07T14:54:38.985
        System.out.println(localDateTime4); // 2022-12-01T14:54:38.985

    }
}
